﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using RiskMan.DataClass.Models;
using TheControllerRisk.Settings;
using TheDoubleStop.Models;
using RiskMan.Properties;
using System.Windows.Documents;
using RiskMan.DataClass;
using RiskMan.DataClass.Models.NotBasModels;

namespace RiskMan.Config
{
    public class History
    {
        private Data _data;

        public History ( DataClass.Data _dataProvider )
        {
            this._data = _dataProvider;
        }

        #region Загрузка

        public void Load ( )
        {
            _data.StopList = GetStopList( );
            _data.DealList.AddRange( GetDealList( ) );
            _data.StopLossOrderList.AddRange( GetStopLossList( ) );

            _data.T2QItems.Clear( );
            var lis = GetQuikSettings();

            for ( int i = 0; i < lis.Count; i++ )
                _data.T2QItems.Add( lis[i] );

            _data.RMItems = GetRMItems( );
            _data.LedderItems = GetLedderItems();
 
        }



        private ObservableCollection<RMItem> GetRMItems ( )
        {
            return ( ObservableCollection<RMItem> ) XMLHistory.GetXmlData( XMLHistory.FILENAME_RMITEMS );
        }

        private ObservableCollection<LedderItem> GetLedderItems()
        {
            return (ObservableCollection<LedderItem>)XMLHistory.GetXmlData(XMLHistory.FILENAME_LEDDER);
        }

        private ObservableCollection<QuikItem> GetQuikSettings ( )
        {
            return ( QuikItems ) XMLHistory.GetXmlData( XMLHistory.FILENAME_QUIKITEMS );
        }

        private List<StopOrderModel> GetStopLossList ( )
        {
            return ( List<StopOrderModel> ) XMLHistory.GetXmlData( XMLHistory.FILENAME_STOPLOSSORDER );
        }

        private List<DealModel> GetDealList ( )
        {
            return ( List<DealModel> ) XMLHistory.GetXmlData( XMLHistory.FILENAME_DEALLIST );
        }

        private ObservableCollection<StopListModel> GetStopList ( )
        {
            return ( ObservableCollection<StopListModel> ) XMLHistory.GetXmlData( XMLHistory.FILENAME_STOPLIST );
        }

        #endregion

        #region Сохранение

        public void Save ( )
        {
            SetStopList( _data.StopList );
            SetDealList( _data.DealSOrderList );
            SetStopLossOrder( _data.StopLossOrderList );
            SetQuikSettings( _data.T2QItems );
            SetRMItems( _data.RMItems );
            SetLedderItems(_data.LedderItems);
        }

        private void SetRMItems ( ObservableCollection<RMItem> observableCollection )
        {
            XMLHistory.SetXmlData( XMLHistory.FILENAME_RMITEMS, ( Object ) ( observableCollection.ToList( ) ) );
        }

        private void SetLedderItems(ObservableCollection<LedderItem> observableCollection)
        {
            XMLHistory.SetXmlData(XMLHistory.FILENAME_LEDDER, (Object)(observableCollection.ToList()));
        }

        private void SetQuikSettings ( QuikItems quikItems )
        {
            XMLHistory.SetXmlData( XMLHistory.FILENAME_QUIKITEMS, ( Object ) quikItems );
        }

        private void SetStopLossOrder ( List<StopOrderModel> list )
        {
            XMLHistory.SetXmlData( XMLHistory.FILENAME_STOPLOSSORDER, ( Object ) list );
        }

        private void SetDealList ( List<DealModel> list )
        {
            XMLHistory.SetXmlData( XMLHistory.FILENAME_DEALLIST, ( Object ) list );
        }

        private void SetStopList ( ObservableCollection<StopListModel> observableCollection )
        {
            XMLHistory.SetXmlData( XMLHistory.FILENAME_STOPLIST, ( Object ) ( observableCollection.ToList( ) ) );
        }

        #endregion
    }
}